﻿using System;
using System.Text;
using System.Data;
using System.Web.UI;
using QzEbook.AccessObject;
using QzEbook.BaseView;
using QzEbook.Common;
using System.Collections.Generic;

namespace qzEbook.webUI
{
    public partial class PageList : UserPage
    {
        private int bookId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SiteUser userInfo= this.getUserInfo();
                Session.Remove("file_info");

                string Bid = Request["Bid"] as string;
                if (Bid == null)
                {
                    Response.StatusCode = 404;
                    Response.Write("没指定具体哪本书");
                    Response.End();
                }
                Session["Bid"] = bookId = int.Parse(Bid);

                quotaLb.Text = string.Format("{0}M<a href='../Feed.xhtml?Bid={1}'><img src='../images/icon_feed.gif' alt='查看源' /></a>", Math.Round(userInfo.Quota - userInfo.Gross, 2), bookId);
                setDefautl();
            }
        }

        private void setDefautl()
        {
            Book book = new Book();
            book.BookId = bookId;
            book= book.select();

            if (book==null)
            {
                Response.StatusCode = 404;
                Response.Write("不存在book的id为" + bookId);
                Response.End();
            }
            //判断当前用户是否可修当然的页面;
            if (book.EditorId != userSession.Uid)
            {
                Response.StatusCode = 404;
                Response.Write("你不能有想篡改别人的作品想法");
                Response.End();
            }

            BookPage page = new BookPage();
            page.BookId = bookId;
            List<BookPage> pageList = page.selectList();

            StringBuilder sb = new StringBuilder();
            sb.Capacity = 2;
            sb.AppendLine("<ul id='pagelist' class='pagelist'>");

            int i = 0;
            foreach (BookPage item in pageList)
            {
                i++;
                sb.AppendLine("<li overClass=\"overbox\" dragClass=\"dragbox\">");

                string imgPath =String.Format("../{0}{1}", Config.UserBookSavePath , item.Path);

                sb.AppendLine("<a href=\"###\">");
                sb.AppendFormat("<img src='getBitmap.xhtml?path={0}' href='{0}' alt='{1}' rel='simplebox' />", imgPath, item.Title);
                sb.AppendLine("</a>");

                sb.AppendLine("<div class='pageItem'>");
                sb.Append("<ins>");
                sb.AppendFormat("<label>大小: </label> {0}  K", item.Bytes);
                sb.AppendFormat("<label>索引: </label><textarea name='index' class='autoSize'>{0}</textarea><br />", item.Index);

                sb.AppendFormat("<textarea name='title' title='标题'>{0}</textarea>", item.Title);

                sb.AppendFormat("<cite><a href='Delete.xhtml?Pid={0}'><img src='../images/delete-page-orange.gif' alt='删除此页' /></a>", item.PageId);
                sb.AppendFormat("<a href='updatePage.xhtml?Pid={0}'><img src='../images/upload-page-orange.gif' alt='保存' /></a></cite>",item.PageId);
                sb.Append("</ins>");
                sb.AppendLine("</div>");
                sb.AppendLine("</li>");
            }
            sb.AppendLine("</ul>");
            pageListLb.Text = sb.ToString();

        }
    }
}